var websocket=null;

//判断当前浏览器是否支持WebSocket
if ('WebSocket' in window) {
  websocket = new WebSocket("ws://localhost:8080/OrderingSystem/websocket");
}
else 
{
  alert('当前浏览器 Not support websocket')
}
websocket.onopen = function () {
	console.log("正在联系商家...");
};

//连接发生错误的回调方法
websocket.onerror = function () {
	console.log("WebSocket连接发生错误");
};
//收到消息的处理方法
websocket.onmessage=function (res){
	console.log(res);
	 var resp= JSON.parse(res.data);//转成js对象
	 console.log(resp.type+resp.mesg);
	 var shopid=resp.shopid;
	 var userid=resp.userid;
	 if(resp.type=="1")
	{
		 layer.open (
						{
					    title : '新订单！',
					    content:'您有一个新订单，是否接受？',
					    
					    area : [
					            '100px', '200px'
					    ],
					    btn : [
					            '接受', '拒绝'
					    ],
					    btn1 : function (index)
					    {
					    	websocket.send(JSON.stringify(
					    			 {
					    			 	"type":"2",
					    			 	"shopid":shopid,
					    			 	"userid":userid
					    			 }
					    			));
					    	layer.close(index);
					    	layer.msg("成功接受订单！",{time: 2000, icon:1});
					    	
					    },
					    btn2 : function (index)
					    {
					    	websocket.send(JSON.stringify(
					    			 {
					    			 	"type":"3",
					    			 	"shopid":shopid,
					    			 	"userid":userid
					    			 }
					    			));
					    	layer.msg("成功拒绝订单！",{time: 2000, icon:1});
					    }
						});
	}
	 if(resp.type=="2")
	{
		 layer.msg("商家已接受您的订单",{time: 2000, icon:1});
	}
	 if(resp.type=="3")
	{
		 layer.msg("商家拒绝了您的订单",{time: 2000, icon:0});
	}
};
